<template>
  <div class="page-container">
    <div class="page-header">
      <h2>最新任务详情</h2>
    </div>

    <div class="tasks-container">
      <!-- 任务概览卡片 -->
      <div class="overview-section">
        <div class="overview-card">
          <div class="overview-icon">📚</div>
          <div class="overview-content">
            <div class="overview-title">本周任务数</div>
            <div class="overview-number">{{ taskStats.weeklyTasks }}</div>
          </div>
        </div>
        <div class="overview-card">
          <div class="overview-icon">✅</div>
          <div class="overview-content">
            <div class="overview-title">已完成任务</div>
            <div class="overview-number">{{ taskStats.completedTasks }}</div>
          </div>
        </div>
        <div class="overview-card">
          <div class="overview-icon">⏳</div>
          <div class="overview-content">
            <div class="overview-title">待完成任务</div>
            <div class="overview-number">{{ taskStats.pendingTasks }}</div>
          </div>
        </div>
      </div>

      <!-- 最新任务列表 -->
      <div class="tasks-list">
        <div v-for="task in latestTasks" :key="task.id" class="task-card">
          <div class="task-header">
            <span class="task-type" :class="task.type">{{ getTaskTypeName(task.type) }}</span>
            <span class="task-status" :class="getStatusClass(task)">
              {{ getStatusText(task) }}
            </span>
          </div>
          <h3 class="task-title">{{ task.title }}</h3>
          <div class="task-info">
            <p class="task-desc">{{ task.description }}</p>
            <div class="task-meta">
              <div class="meta-item">
                <i class="icon">📅</i>
                <span>开始时间：{{ formatDate(task.startTime) }}</span>
              </div>
              <div class="meta-item">
                <i class="icon">⏰</i>
                <span>截止时间：{{ formatDate(task.endTime) }}</span>
              </div>
            </div>
          </div>

          <!-- 学习资源 -->
          <div class="task-resources" v-if="task.resources && task.resources.length">
            <h4>学习资源</h4>
            <div class="resource-list">
              <a v-for="resource in task.resources" :key="resource.url" :href="resource.url" class="resource-item" target="_blank">
                <i class="icon">📄</i>
                <span>{{ resource.name }}</span>
              </a>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { taskApi } from '@/api'

export default {
  name: 'ParentTaskAnalysis',
  data() {
    return {
      latestTasks: [],
      taskStats: {
        weeklyTasks: 0,
        completedTasks: 0,
        pendingTasks: 0
      }
    }
  },
  async created() {
    await this.fetchTasks()
    this.calculateStats()
  },
  methods: {
    async fetchTasks() {
      try {
        const response = await taskApi.getTasks()
        this.latestTasks = response.data.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt)).slice(0, 5) // 只显示最新的5个任务
      } catch (error) {
        console.error('获取任务失败:', error)
        this.$message.error('获取任务失败')
      }
    },
    calculateStats() {
      const now = new Date()
      const weekStart = new Date(now.setDate(now.getDate() - now.getDay()))

      this.taskStats.weeklyTasks = this.latestTasks.filter((task) => new Date(task.createdAt) >= weekStart).length

      this.taskStats.completedTasks = this.latestTasks.filter((task) => new Date(task.endTime) < new Date()).length

      this.taskStats.pendingTasks = this.latestTasks.length - this.taskStats.completedTasks
    },
    getTaskTypeName(type) {
      const typeMap = {
        vocabulary: '词汇练习',
        reading: '阅读理解',
        listening: '听力训练',
        speaking: '口语练习',
        writing: '写作任务'
      }
      return typeMap[type] || type
    },
    getStatusClass(task) {
      const now = new Date()
      const endTime = new Date(task.endTime)
      return {
        completed: endTime < now,
        ongoing: endTime >= now
      }
    },
    getStatusText(task) {
      return this.getStatusClass(task).completed ? '已完成' : '进行中'
    },
    formatDate(dateStr) {
      const date = new Date(dateStr)
      return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')} ${String(
        date.getHours()
      ).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}`
    }
  }
}
</script>

<style scoped>
.page-container {
  padding: 20px;
}

.page-header {
  margin-bottom: 24px;
}

.overview-section {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 24px;
  margin-bottom: 32px;
}

.overview-card {
  background: rgba(226, 223, 223, 0.616);
  border-radius: 8px;
  padding: 20px;
  display: flex;
  align-items: center;
  gap: 16px;
}

.overview-icon {
  font-size: 32px;
  color: #409eff;
}

.overview-content {
  flex: 1;
}

.overview-title {
  font-size: 14px;
  color: #909399;
  margin-bottom: 8px;
}

.overview-number {
  font-size: 24px;
  color: #303133;
  font-weight: 500;
}

.tasks-list {
  display: grid;
  gap: 24px;
}

.task-card {
  background: white;
  border-radius: 8px;
  padding: 20px;
  border: 1px solid #dcdfe6;
}

.task-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 16px;
}

.task-type {
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  color: white;
}

.vocabulary {
  background: #409eff;
}
.reading {
  background: #67c23a;
}
.listening {
  background: #e6a23c;
}
.speaking {
  background: #f56c6c;
}
.writing {
  background: #909399;
}

.task-status {
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
}

.completed {
  background: #f0f9eb;
  color: #67c23a;
}

.ongoing {
  background: #fdf6ec;
  color: #e6a23c;
}

.task-title {
  margin: 0 0 16px 0;
  font-size: 18px;
  color: #303133;
}

.task-desc {
  margin: 0 0 16px 0;
  color: #606266;
  line-height: 1.6;
}

.task-meta {
  display: grid;
  gap: 12px;
  margin-bottom: 20px;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #909399;
  font-size: 14px;
}

.task-resources h4 {
  margin: 0 0 12px 0;
  font-size: 16px;
  color: #303133;
}

.resource-list {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

.resource-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: #f5f7fa;
  border-radius: 4px;
  color: #606266;
  text-decoration: none;
  transition: all 0.3s;
}

.resource-item:hover {
  background: #ecf5ff;
  color: #409eff;
}

@media (max-width: 768px) {
  .overview-section {
    grid-template-columns: 1fr;
  }
}
</style>
